package com.bw.attendance.sign

import android.view.LayoutInflater
import androidx.activity.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.facade.annotation.Route
import com.baidu.location.*
import com.baidu.mapapi.CoordType
import com.baidu.mapapi.SDKInitializer
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import com.bw.attendance.databinding.ActivitySignBinding
import com.bw.attendance.sign.entity.SignEntity
import com.bw.baseapplication.BaseApplication
import com.bw.common.mvi.BaseActivity
import com.bw.widget.decoration.SignLineDecoration
import com.bw.widget.header.MDMRefreshHeader
import kotlinx.coroutines.launch
import java.util.*

@Route(path = "/attendance/signactivity")
class SignActivity : BaseActivity<ActivitySignBinding,SignViewModel>() {

    private lateinit var mLocationClient : LocationClient
    private val myListener = MyLocationListener()
    private lateinit var addressStr : String
    private var lat :Double=0.0
    private var longitude :Double=0.0
    private lateinit var adapter: SignAdapter

    override fun createVM(): SignViewModel {
        return viewModels<SignViewModel> ().value
    }

    override fun createViewBinding(): ViewBinding {
        return ActivitySignBinding.inflate(LayoutInflater.from(this))
    }

    override fun initView() {
        initLocation()
        v.signBtn.text = "签到"
        v.signBar.titleTv.text = "考勤"
        v.signDateTv.text = "时间"+TimeUtils.getChineseWeek(System.currentTimeMillis())
        v.signBar.backImg.setOnClickListener { finish() }
        v.signBtn.setOnClickListener {
            vm.requestSign(1,lat,longitude)
        }
        val manager = LinearLayoutManager(this)
        manager.orientation = LinearLayoutManager.VERTICAL
        v.signRv.layoutManager = manager
        v.signRv.addItemDecoration(SignLineDecoration(this))
        adapter = SignAdapter()
        v.signRv.adapter= adapter
    }

    override fun initData() {
        vm.requestSignHistory(1)
    }

    private fun initLocation(){
        // 默认本地个性化地图初始化方法
        SDKInitializer.initialize(BaseApplication.baseApplication)
        SDKInitializer.setCoordType(CoordType.BD09LL)
        mLocationClient = LocationClient(BaseApplication.baseApplication)
        mLocationClient.registerLocationListener(myListener)
        val option = LocationClientOption()
        option.setIsNeedAddress(true)
        option.setCoorType("bd09ll")
        option.setEnableSimulateGps(true)
        mLocationClient.locOption = option
        mLocationClient.start()
    }

    override fun onDestroy() {
        super.onDestroy()
        mLocationClient.stop()
    }

    override fun bindState() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED){
                vm.mutableContainer.uiStateFlow.collect{
                    if (it.dataState&& it.data.isNotEmpty()){
                        //获取数据返回状态
                        adapter.setNewInstance(it.data)
                    }
                    if (it.signState){
                        //签到返回状态
                        if (it.sign.msg.equals("成功"))
                            finish()
                    }

                }
            }
        }
    }

    inner class MyLocationListener : BDAbstractLocationListener(){
        override fun onReceiveLocation(location: BDLocation?) {
            if (location == null)
                mLocationClient.start()
            if (location!!.locType == 167){
                addressStr = "天津"
                return
            }
            addressStr = location.address.address
            lat = location.latitude
            longitude = location.longitude
        }

    }

}